<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/12/2
 * Time: 19:05
 */
namespace app\autoreply\controller;

use app\common\config\autoreply_config;
use app\common\helper\appmsg_helper;
use app\common\helper\autoreply_helper;
use app\common\model\Appmsg;
use think\facade\Config;
use think\facade\View;
use think\facade\Request;
use app\admin\controller\AdminBase;

class Add extends AdminBase
{
    private $autoreply_type;
    private $autoreply_content_type;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub

        $this->autoreply_type        = autoreply_config::$autoreply_type;
        $this->autoreply_content_type = autoreply_config::$autoreply_content_type;
    }

    public function __call($action = '', $params = array())
    {
        $content_type = Request::param('content_type', 1);

        if (!array_key_exists($action.'', $this->autoreply_type)) {
            $action = 1;
        }
        if (!array_key_exists($content_type.'', $this->autoreply_content_type)) {
            $content_type = 0;
        }

        View::assign([
            'autoreply_type' => $action,
            'content_type'   => $content_type
        ]);

        if ($action == 4) {
            return View::fetch('activitykey_add');
        }

        // 关键词的回复
        if ($action == 3) {
            //@todo 取当前这个关键词的信息
            $id = Request::param('id', 0);
            if ($id) {
                $this->edit_autokey($id);
            }

            return View::fetch('autokey_add');

            // 关注时的回复or不能识别时的回复
        } else {
            $content_info = autoreply_helper::get_autoreply_content_by_type($action, 1, $this->open_card_id, $this->company_id);

            View::assign('content_info', $content_info);
            if ($content_info) {
                View::assign('select_content_type', $content_info['content_type']);
            }

            return View::fetch('add_autoreply');
        }
    }

    private function edit_autokey($id)
    {
        $key_info = _uri('autoreply', ['id' => $id]);
        View::assign('key_info', $key_info);
    }

}